<?php
/**
 * 用户权限分配功能模块
 * @author Administrator
 *
 */
class UserModel extends Model{
	/**
	 * 根据用户名查找用户信息
	 * @param unknown $user_name
	 * @return Ambigous <boolean, multitype:>
	 */
	function getUserInfo($user_name){
		$user_name = $this->db->escape_string($user_name);
		$sql = "select user_id,user_name,user_pass,user_privileges,role_id,is_admin from admin where user_name='{$user_name}' limit 1";
		return $this->db->getRecord($sql);
	}
	/**
	 * 根据用户id获取用户信息
	 * @param int $user_id
	 * @return boolean|Ambigous <boolean, multitype:>
	 */
	function getUserInfoById($user_id){
		if(empty($user_id) || !is_numeric($user_id)){
			return false;
		}
		$sql = "select user_id,user_name,user_pass,user_privileges,role_id,is_admin from admin where user_id={$user_id}";
		return $this->db->getRecord($sql);
	}
	/**
	 * 获取用户所有 父权限
	 */
	function getParPrivileges(){
		$sql = "select * from admin_privilege where pid=0 order by id desc";
		return $this->db->getRecords($sql);
	}
	
	function getChrPrivleges($pid){
		$sql = "select * from admin_privilege where pid={$pid}";
		return $this->db->getRecords($sql);
	}
	/**
	 * 根据子权限获取父权限
	 * @param string $privilege 用,分割
	 */
	function getParentPrivileges($privilege){
		$sql = "select distinct pid from admin_privilege where id in({$privilege})";
		return $this->db->getRecords($sql);
	}
	/**
	 * 获取角色信息
	 * @return Ambigous <multitype:, multitype:multitype: >
	 */
	function getRoleInfo(){
		$sql = "select role_id,role_name,channel_min,channel_max from admin_role";
		return $this->db->getRecords($sql);
	}
	
	/**
	 * 更加角色id获取角色信息
	 * @param int $role_id
	 */
	function getRoleInfoById($role_id){
		if(empty($role_id) || !is_numeric($role_id)){
			return false;
		}
		$sql = "select role_id,role_name,channel_min,channel_max from admin_role where role_id={$role_id}";
		return $this->db->getRecord($sql);
	}
	
}